<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货仓库" prop="delivery_id">
        <el-select v-model="queryParams.delivery_id" clearable placeholder="请选择">
          <el-option
            v-for="item in deliveryName"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="收货仓库" prop="receive_id">
        <el-select v-model="queryParams.receive_id" clearable placeholder="请选择">
          <el-option
            v-for="item in receiveName"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:order:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:order:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:order:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:order:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="单据号" align="center" prop="id">
        <template slot-scope="scope">
          <router-link :to="{path:'/wms/orderbom',query:{'parent_code':scope.row.id}}" class="link-type">
            <span>{{ scope.row.id }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="order_type_id">
      </el-table-column>
      <el-table-column label="发货仓库" align="center" prop="delivery_id"/>
      <el-table-column label="收货仓库" align="center" prop="receive_id"/>
      <el-table-column label="单据状态" align="center" prop="state"/>
      <el-table-column label="生产厂商" align="center" prop="manufacturer_id"/>
      <el-table-column label="申请人" align="center" prop="user_applicant_id"/>
      <el-table-column label="审核人" align="center" prop="user_auditor_id"/>
      <el-table-column label="发货员" align="center" prop="user_delivery_id"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:order:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:order:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleExecute(scope.row)"
            v-hasPermi="['wms:order:remove']"
          >执行
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发货仓库" prop="delivery_id">
          <el-select v-model="form.delivery_id" clearable placeholder="请选择发货仓库">
            <el-option
              v-for="item in listName"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货仓库" prop="receive_id">
          <el-select v-model="form.receive_id" clearable placeholder="请选择收货仓库">
            <el-option
              v-for="item in listName"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产厂商" prop="manufacturer_id">
          <el-input v-model="form.manufacturer_id" placeholder="请输入生产厂商"/>
        </el-form-item>
        <el-form-item label="申请人" prop="user_applicant_id">
          <el-input v-model="form.user_applicant_id" placeholder="请输入申请人"/>
        </el-form-item>
        <el-form-item label="审核人" prop="user_auditor_id">
          <el-input v-model="form.user_auditor_id" placeholder="请输入审核人"/>
        </el-form-item>
        <el-form-item label="发货员" prop="user_delivery_id">
          <el-input v-model="form.user_delivery_id" placeholder="请输入发货员"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  getOrderListDeliveryName,
  getOrderListReceiveName,
  getOrderListName, updateOrderStatus, getOrderType
} from "@/api/wms/order";
import dictSelect from "../../components/dictSelect.vue"

export default {
  name: "Order",
  dicts: ["sys_yes_no", "bd_goods_measure_unit"],
  components: {dictSelect},
  data() {
    return {
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      OrderTypes: {},
      deliveryName: {},
      receiveName: {},
      listName: {},
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      dict_type: undefined,
      // 总条数
      total: 0,
      // 物品订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        order_type_id: this.$route.query && this.$route.query.order_type_id,
        delivery_id: null,
        receive_id: null,
        state: null,
        source_id: null,
        manufacturer_id: null,
        user_applicant_id: null,
        user_auditor_id: null,
        user_delivery_id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        delivery_id: [
          {required: true, message: "出货仓库不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getOrderListName();
    this.getAreaWarehouse();
    this.getOrderType();
  },
  methods: {
    getOrderType() {
      getOrderType().then(res => {
        this.OrderTypes = res.data
      })
    },
    handleGoodsSelect(param) {
      console.log(param)
      var dictParams = {
        dict_type: param
      }
      this.$refs.dictSelect.show(dictParams)
    },
    getOrderListName() {
      getOrderListName().then(res => {
        this.listName = res.data
      })
    },
    getOrderListDeliveryName() {
      getOrderListDeliveryName().then(res => {
        this.deliveryName = res.data
      })
    },
    getOrderListReceiveName() {
      getOrderListReceiveName().then(res => {
        this.receiveName = res.data
      })
    },
    /** 查询物品订单列表 */
    getList() {
      console.log("this.order_type_id", this.order_type_id)
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getOrderType();
      this.getOrderListDeliveryName();
      this.getOrderListReceiveName();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delivery_id: null,
        receive_id: null,
        quantity: 0,
        order_type_id: this.$route.query && this.$route.query.order_type_id,
        type: this.$route.query && this.$route.query.type,
        goods_code: null,
        area_id: null,
        state: null,
        source_id: null,
        manufacturer_id: null,
        user_applicant_id: null,
        user_auditor_id: null,
        user_delivery_id: null,
        remark: null,
        create_by: null,
        create_time: null,
        update_by: null,
        update_time: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物品订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    handleExecute(row) {
      const id = row.id;
      this.$modal.confirm('是否执行出库物品订单编号为"' + id + '"的数据项？').then(function () {
        return updateOrderStatus(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("出库成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
